# -*- coding: UTF-8 -*-
# 定义扎德算子，以及其他运算类
import numpy as np

from business.logger import Logger

# 初始化日志
log = Logger("zardehoperator.py").logger


class ZardehOperator:

    def ZardehOperator(self):
        pass

    # 模糊矩阵在分段求小后进行取大运算
    @staticmethod
    def sumMax(matrix=[]):
        matrix.sort(reverse=True)
        return matrix[0]

    # 扎德算子取小
    @staticmethod
    def zMin(a, b):
        if a <= b:
            c = a
        else:
            c = b
        return c

    # 扎德算子取大
    @staticmethod
    def zMax(a, b):
        if a <= b:
            c = b
        else:
            c = a
        return c

    # 分割列表，对总元素数为m个的列表，每隔n个元素进行分割，返回（m/n）或者（（m/n）+1）个列表
    @staticmethod
    def splitList(n, array_list=[]):
        counter = array_list.__len__()
        array_list.reverse()
        array_set = []
        tempArg = []
        if n == 1:
            for x in range(counter):
                tempArg.append(array_list.pop())
                arg = tempArg.copy()
                array_set.append(arg)
                tempArg.clear()
        else:
            tempArg = [array_list.pop()]
            for x in range(1, counter):
                tempArg.append(array_list.pop())
                if (x + 1) % n == 0:
                    argosy = tempArg.copy()
                    array_set.append(argosy)
                    tempArg.clear()
                    continue
            if tempArg.__len__() != 0:
                array_set.append(tempArg)
        return array_set

    # 计算闭包矩阵函数
    @staticmethod
    def transitiveMatrix(element, tms=[]):
        log.info("原始矩阵：")
        log.info(tms)
        temp = []
        temp1 = []
        for i in range(len(tms)):
            for j in range(len(tms[0])):
                for k in range(len(tms)):
                    a = tms[i][k]
                    b = tms[k][j]
                    temp.append(ZardehOperator.zMin(a, b))
                temp1.append(ZardehOperator.sumMax(temp))
                temp.clear()
        newFArg = np.array(ZardehOperator.splitList(element, temp1))
        log.info("闭包一次后矩阵：")
        log.info(newFArg)
        return newFArg
